home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 23 / CU Amiga - Super CD-ROM 23 (June 1998).iso / CreatingGames / Utilities / C / Mesa / samples / blendeq.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-01-29  |  7.0 KB  |  297 lines

  1. /*
  2. ** blendeq.c - Demonstrates the use of the blend_minmax, blend_subtract,
  3. **    and blend_logic_op extensions using glBlendEquationEXT.
  4. **
  5. **    Over a two-color backround, draw rectangles using twelve blend
  6. **    options.  The values are read back as UNSIGNED_BYTE and printed
  7. **    in hex over each value.  These values are useful for logic
  8. **    op comparisons when channels are 8 bits deep.
  9. */
  10.  
  11. #include <string.h>
  12. #include <unistd.h>
  13. #include <stdlib.h>
  14. #include <stdio.h>
  15. #include "gltk.h"
  16.  
  17.  
  18. GLenum doubleBuffer, directRender;
  19. static int dithering = 0;
  20. static int doPrint = 1;
  21. static int deltaY;
  22. GLint windW, windH;
  23. GLuint bitmapBase;
  24.  
  25. static void Init(void)
  26. {
  27.     bitmapBase = glGenLists(256);
  28.     if (tkCreateBitmapFont(bitmapBase) == GL_FALSE) {
  29.     tkQuit();
  30.     }
  31.  
  32.     glDisable(GL_DITHER);
  33.     glShadeModel(GL_FLAT);
  34. }
  35.  
  36. static void Reshape(int width, int height)
  37. {
  38.  
  39.     windW = (GLint)width;
  40.     windH = (GLint)height;
  41.  
  42.     glViewport(0, 0, (GLint)width, (GLint)height);
  43.     deltaY = windH /16;
  44.  
  45.     glMatrixMode(GL_PROJECTION);
  46.     glLoadIdentity();
  47.     gluOrtho2D(0, windW, 0, windH);
  48.     glMatrixMode(GL_MODELVIEW);
  49. }
  50.  
  51. static GLenum Key(int key, GLenum mask)
  52. {
  53.  
  54.     switch (key) {
  55.       case TK_ESCAPE:
  56.     tkQuit();
  57.       case TK_d:
  58.     dithering = !dithering;
  59.     break;
  60.       default:
  61.     return GL_FALSE;
  62.     }
  63.     return GL_TRUE;
  64. }
  65.  
  66. static void PrintColorStrings( void )
  67. {
  68.     GLubyte ubbuf[3];
  69.     int i, xleft, xright;
  70.     char colorString[18];
  71.  
  72.     xleft = 5 + windW/4;
  73.     xright = 5 + windW/2;
  74.  
  75.     for (i = windH - deltaY + 4; i > 0; i-=deltaY) {
  76.         glReadPixels(xleft, i+10, 1, 1, GL_RGB, GL_UNSIGNED_BYTE, ubbuf);
  77.         sprintf(colorString, "(0x%x, 0x%x, 0x%x)",
  78.                 ubbuf[0], ubbuf[1], ubbuf[2]);
  79.         glRasterPos2f(xleft, i);
  80.     tkDrawStr(bitmapBase, colorString);
  81.         glReadPixels(xright, i+10, 1, 1, GL_RGB, GL_UNSIGNED_BYTE, ubbuf);
  82.         sprintf(colorString, "(0x%x, 0x%x, 0x%x)",
  83.                 ubbuf[0], ubbuf[1], ubbuf[2]);
  84.         glRasterPos2f(xright, i);
  85.     tkDrawStr(bitmapBase, colorString);
  86.     }
  87. }
  88.  
  89. static void Draw(void)
  90. {
  91.     int stringOffset = 5, stringx = 8;
  92.     int x1, x2, xleft, xright;
  93.     int i;
  94.  
  95.     (dithering) ? glEnable(GL_DITHER) : glDisable(GL_DITHER);
  96.     glDisable(GL_BLEND);
  97.  
  98.     glClearColor(0.5, 0.6, 0.1, 1.0);
  99.     glClear(GL_COLOR_BUFFER_BIT);
  100.  
  101.     /* Draw background */
  102.     glColor3f(0.1, 0.1, 1.0);
  103.     glRectf(0.0, 0.0, windW/2, windH);
  104.  
  105.     /* Draw labels */
  106.     glColor3f(0.8, 0.8, 0.0);
  107.     i = windH - deltaY + stringOffset;
  108.     glRasterPos2f(stringx, i); i -= deltaY;
  109.     tkDrawStr(bitmapBase, "SOURCE");
  110.     glRasterPos2f(stringx, i); i -= deltaY;
  111.     tkDrawStr(bitmapBase, "DEST");
  112.     glRasterPos2f(stringx, i); i -= deltaY;
  113.     tkDrawStr(bitmapBase, "min");
  114.     glRasterPos2f(stringx, i); i -= deltaY;
  115.     tkDrawStr(bitmapBase, "max");
  116.     glRasterPos2f(stringx, i); i -= deltaY;
  117.     tkDrawStr(bitmapBase, "subtract");
  118.     glRasterPos2f(stringx, i); i -= deltaY;
  119.     tkDrawStr(bitmapBase, "reverse_subtract");
  120.     glRasterPos2f(stringx, i); i -= deltaY;
  121.     tkDrawStr(bitmapBase, "clear");
  122.     glRasterPos2f(stringx, i); i -= deltaY;
  123.     tkDrawStr(bitmapBase, "set");
  124.     glRasterPos2f(stringx, i); i -= deltaY;
  125.     tkDrawStr(bitmapBase, "copy");
  126.     glRasterPos2f(stringx, i); i -= deltaY;
  127.     tkDrawStr(bitmapBase, "noop");
  128.     glRasterPos2f(stringx, i); i -= deltaY;
  129.     tkDrawStr(bitmapBase, "and");
  130.     glRasterPos2f(stringx, i); i -= deltaY;
  131.     tkDrawStr(bitmapBase, "invert");
  132.     glRasterPos2f(stringx, i); i -= deltaY;
  133.     tkDrawStr(bitmapBase, "or");
  134.     glRasterPos2f(stringx, i); i -= deltaY;
  135.     tkDrawStr(bitmapBase, "xor");
  136.  
  137.  
  138.     i = windH - deltaY;
  139.     x1 = windW/4;
  140.     x2 = 3 * windW/4;
  141.     xleft = 5 + windW/4;
  142.     xright = 5 + windW/2;
  143.  
  144.     /* Draw foreground color for comparison */
  145.     glColor3f(0.9, 0.2, 0.8);
  146.     glRectf(x1, i, x2, i+deltaY);
  147.  
  148.     /* Leave one rectangle of background color */
  149.  
  150.     /* Begin test cases */
  151.     glEnable(GL_BLEND);
  152.     glBlendFunc(GL_ONE, GL_ONE);
  153.  
  154.     i -= 2*deltaY;
  155.     glBlendEquationEXT(GL_MIN_EXT);
  156.     glRectf(x1, i, x2, i+deltaY);
  157.  
  158.     i -= deltaY;
  159.     glBlendEquationEXT(GL_MAX_EXT);
  160.     glRectf(x1, i, x2, i+deltaY);
  161.  
  162.     i -= deltaY;
  163.     glBlendEquationEXT(GL_FUNC_SUBTRACT_EXT);
  164.     glRectf(x1, i, x2, i+deltaY);
  165.  
  166.     i -= deltaY;
  167.     glBlendEquationEXT(GL_FUNC_REVERSE_SUBTRACT_EXT);
  168.     glRectf(x1, i, x2, i+deltaY);
  169.  
  170.     glBlendFunc(GL_ONE, GL_ZERO);
  171.     i -= deltaY;
  172.     glBlendEquationEXT(GL_LOGIC_OP);
  173.     glLogicOp(GL_CLEAR);
  174.     glRectf(x1, i, x2, i+deltaY);
  175.  
  176.     i -= deltaY;
  177.     glBlendEquationEXT(GL_LOGIC_OP);
  178.     glLogicOp(GL_SET);
  179.     glRectf(x1, i, x2, i+deltaY);
  180.  
  181.     i -= deltaY;
  182.     glBlendEquationEXT(GL_LOGIC_OP);
  183.     glLogicOp(GL_COPY);
  184.     glRectf(x1, i, x2, i+deltaY);
  185.  
  186.     i -= deltaY;
  187.     glBlendEquationEXT(GL_LOGIC_OP);
  188.     glLogicOp(GL_NOOP);
  189.     glRectf(x1, i, x2, i+deltaY);
  190.  
  191.     i -= deltaY;
  192.     glBlendEquationEXT(GL_LOGIC_OP);
  193.     glLogicOp(GL_AND);
  194.     glRectf(x1, i, x2, i+deltaY);
  195.  
  196.     i -= deltaY;
  197.     glBlendEquationEXT(GL_LOGIC_OP);
  198.     glLogicOp(GL_INVERT);
  199.     glRectf(x1, i, x2, i+deltaY);
  200.  
  201.     i -= deltaY;
  202.     glBlendEquationEXT(GL_LOGIC_OP);
  203.     glLogicOp(GL_OR);
  204.     glRectf(x1, i, x2, i+deltaY);
  205.  
  206.     i -= deltaY;
  207.     glBlendEquationEXT(GL_LOGIC_OP);
  208.     glLogicOp(GL_XOR);
  209.     glRectf(x1, i, x2, i+deltaY);
  210.     glRectf(x1, i+10, x2, i+5);
  211.  
  212.   if (doPrint) {
  213.       glDisable(GL_BLEND);
  214.       glColor3f(1.0, 1.0, 1.0);
  215.       PrintColorStrings();
  216.   }
  217.   glFlush();
  218.  
  219.     if (doubleBuffer) {
  220.        tkSwapBuffers();
  221.     }
  222.  
  223. }
  224.  
  225. static GLenum Args(int argc, char **argv)
  226. {
  227.     GLint i;
  228.  
  229.     doubleBuffer = GL_FALSE;
  230.     directRender = GL_TRUE;
  231.  
  232.     for (i = 1; i < argc; i++) {
  233.     if (strcmp(argv[i], "-sb") == 0) {
  234.         doubleBuffer = GL_FALSE;
  235.     } else if (strcmp(argv[i], "-db") == 0) {
  236.         doubleBuffer = GL_TRUE;
  237.     } else if (strcmp(argv[i], "-dr") == 0) {
  238.         directRender = GL_TRUE;
  239.     } else if (strcmp(argv[i], "-ir") == 0) {
  240.         directRender = GL_FALSE;
  241.     } else {
  242.         printf("%s (Bad option).\n", argv[i]);
  243.         return GL_FALSE;
  244.     }
  245.     }
  246.     return GL_TRUE;
  247. }
  248.  
  249. void main(int argc, char **argv)
  250. {
  251.     GLenum type;
  252.     char *s;
  253.     char *extName1 = "GL_EXT_blend_logic_op";
  254.     char *extName2 = "GL_EXT_blend_minmax";
  255.     char *extName3 = "GL_EXT_blend_subtract";
  256.  
  257.     if (Args(argc, argv) == GL_FALSE) {
  258.     tkQuit();
  259.     }
  260.  
  261.     tkInitPosition(0, 0, 800, 400);
  262.  
  263.     type = TK_RGB;
  264.     type |= (doubleBuffer) ? TK_DOUBLE : TK_SINGLE;
  265.     type |= (directRender) ? TK_DIRECT : TK_INDIRECT;
  266.     tkInitDisplayMode(type);
  267.  
  268.     if (tkInitWindow("Blend Equation") == GL_FALSE) {
  269.     tkQuit();
  270.     }
  271.  
  272.     /* Make sure blend_logic_op extension is there. */
  273.     s = (char *) glGetString(GL_EXTENSIONS);
  274.     if (!s)
  275.     tkQuit();
  276.     if (strstr(s,extName1) == 0) {
  277.     printf("Blend_logic_op extension is not present.\n");
  278.     tkQuit();
  279.     }
  280.     if (strstr(s,extName2) == 0) {
  281.     printf("Blend_minmax extension is not present.\n");
  282.     tkQuit();
  283.     }
  284.     if (strstr(s,extName3) == 0) {
  285.     printf("Blend_subtract extension is not present.\n");
  286.     tkQuit();
  287.     }
  288.  
  289.     Init();
  290.  
  291.     tkExposeFunc(Reshape);
  292.     tkReshapeFunc(Reshape);
  293.     tkKeyDownFunc(Key);
  294.     tkDisplayFunc(Draw);
  295.     tkExec();
  296. }
  297.